﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nate_sRoguelikeGameXNA
{
    class MapBlockPathInfo : IComparable<MapBlockPathInfo>
    {
        # region Fields

        public int DistanceFromStart, DistanceToEnd, TotalDistance;
        public MapBlock MapBlock;
        public MapBlockPathInfo Parent = null;
        int eX, eY;
        public int LastActiveIteration = 0;
        # endregion

        # region Constructor
        public MapBlockPathInfo(MapBlock block, MapBlockPathInfo parent, int eX, int eY, int iteration)
        {
            Parent = parent;
            MapBlock = block;
            this.eX = eX;
            this.eY = eY;
            this.LastActiveIteration = iteration;
            CalculateDistances();
        }

        public MapBlockPathInfo(MapBlock currentBlock, MapBlockPathInfo parent, MapBlock targetBlock, int iteration)
            :this(currentBlock, parent, targetBlock.Location.X, targetBlock.Location.Y, iteration)
        {
            
        }

        # endregion

        # region Methods

        public void CalculateDistances()
        {
            DistanceFromStart = Parent == null ? 0 : Parent.DistanceFromStart + CalculateDistanceFromAdjacentBlock(Parent.MapBlock);
            DistanceToEnd = (Math.Abs(eX - MapBlock.Location.X) + Math.Abs(eY - MapBlock.Location.Y)) * 10;
            TotalDistance = DistanceFromStart + DistanceToEnd;
        }

        public int CalculateDistanceFromAdjacentBlock(MapBlock b)
        {
            int sX = b.Location.X;
            int sY = b.Location.Y;

            int distance = Math.Abs(sX - MapBlock.Location.X) + Math.Abs(sY - MapBlock.Location.Y);

            if (distance >= 2)
            {
                if (distance == 2 && sX != MapBlock.Location.X && sY != MapBlock.Location.Y)
                {
                    // If neither x or y is the same as sX or sY and the distance between points (x,y) and (sX, sY) is 2,
                    // then (x,y) is located diagonally from (sX, sY)
                    distance = 54;
                }
                else
                {
                    // If the distance > 2 or the block is not a diagonal, the blocks are not adjacent to each other.
                    distance = -1;
                }
            }
            else
            {
                // The block is directly next to each other (no diagonals)
                distance = 10;
            }

            return distance;
        }

        public int CompareTo(MapBlockPathInfo otherPath)
        {
            // If the distance is shorter for this path, return -1. If they're the same, 0. If it's greater, 1.
            int result = ((int)this.TotalDistance).CompareTo((int)otherPath.TotalDistance);
            if (result == 0) // If the result was 0, perform an inverse compare on the lastActiveIteration field.
            // If this path object was active after the other, return -1. If the other was active last, return 1.
            {
                result = otherPath.LastActiveIteration.CompareTo(this.LastActiveIteration);
            }
            return result;
        }

        //public void paint(Graphics g) {
        //    g.setColor(Color.white);
        //    g.drawString(String.format("%d", TotalDistance), MapBlock.X + 4, MapBlock.Y + 3 + g.getFont().getSize());
        //    g.drawString(String.format("%d", DistanceFromStart), MapBlock.X + 4, MapBlock.Y + MapBlock.Height - 4);
        //    g.drawString(String.format("%d", DistanceToEnd), MapBlock.X + MapBlock.Width - 13, MapBlock.Y + MapBlock.Height - 4);
        //}
        # endregion

    }

}
